package com.yunkeji.config.keymonitor.config;

import lombok.SneakyThrows;
import org.redisson.api.RedissonClient;
import org.redisson.spring.starter.RedissonAutoConfiguration;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;

import javax.annotation.PostConstruct;

/**
 * rediskey监听
 *
 * @author 老张
 */
@Configuration
public class RedisListenerConfig {

    private static Integer REDIS_EXPIRE_DB = 0;
    private static RedissonClient EXPIRE_DB;
    @Autowired private RedisConnectionFactory redisConnectionFactory;

    @Autowired private RedisListenerHandel keyExpiredListener;

    @Autowired private ObjectProvider<RedissonAutoConfiguration> redissonAutoConfiguration;
    @Autowired private ObjectProvider<RedisProperties> redisProperties;

    @Autowired private RedissonAutoConfiguration ddddd;

    @Autowired private ApplicationContext applicationContext;

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainerDev() {
        RedisMessageListenerContainer redisMessageListenerContainer =
                new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        // 配置监听器监听的主题关联
        Topic topic7 = new ChannelTopic("__keyevent@" + REDIS_EXPIRE_DB + "__:expired");
        redisMessageListenerContainer.addMessageListener(keyExpiredListener, topic7);
        return redisMessageListenerContainer;
    }

    /**
     * 获取过期专用DB
     *
     * @author 老张
     */
    public static RedissonClient getExpireDb() {
        return EXPIRE_DB;
    }

    @Lookup
    public RedissonAutoConfiguration getNewAnotherBean() {
        // Spring 会自动注入一个新的 AnotherBean 实例
        return null; // 方法体不需要实际实现，Spring 会自动替换
    }
    /**
     * 专门处理redis过期任务Db
     *
     * @author 老张
     */
    @SneakyThrows
    @PostConstruct
    public void configExpireDb() {
        //        RedisProperties rp = redisProperties.getObject();
        //        rp.setDatabase(REDIS_EXPIRE_DB);
        //
        //        RedissonAutoConfiguration object = redissonAutoConfiguration.getObject();
        //
        //        Field redisProperties =
        //                ReflectUtils.getField(RedissonAutoConfiguration.class, "redisProperties");
        //        redisProperties.setAccessible(true);
        //        redisProperties.set(object, rp);
        //        RedissonClient redisson = (RedissonClient) ReflectUtils.invoke(object,
        // "redisson");
        //        System.err.println(redisson);
        //        EXPIRE_DB = redisson;
        //
        //        RedissonAutoConfiguration bean =
        //                applicationContext.getBean(RedissonAutoConfiguration.class);
        //        RedissonClient redisson1 = bean.redisson();
        //        System.err.println(redisson1);
        //
        //        RedissonAutoConfiguration newAnotherBean = getNewAnotherBean();
        //        RedissonClient redisson2 = newAnotherBean.redisson();
        //        System.err.println(redisson2);
        //        EXPIRE_DB = redisson2;
        //        RedissonConfig config = new RedissonConfig();
        //        for (int i = 0; i < 16; i++) { // 假设有16个数据库
        //            redissonClients.put(i, config.createRedissonClient(i));
        //        }
        //        RedisUtils.setCacheObject(
        //                RedisListenerConfig.getExpireDb(), "ddddd",
        // "nisfkabkdbaw卡红包v的健康拉黑吧未打卡垃圾号", false);
    }
}
